﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;


namespace FDeal
{
    public class TaiKhoanDoanhNghiepDAO : DataProvider
    {
        public TaiKhoanDoanhNghiepDAO()
        { 
        }

        public TaiKhoanDoanhNghiepDTO[] getDsTaiKhoanDoanhNghiep()
        {
            connect();
            string query = "SELECT * FROM TAIKHOANDOANHNGHIEP";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            TaiKhoanDoanhNghiepDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new TaiKhoanDoanhNghiepDTO[n];
            for (i = 0; i < n; i++)
            {
                object lop = GetDataFromDataRow(dt, i);
                arr[i] = (TaiKhoanDoanhNghiepDTO)lop;
            }
            return arr;
        }



        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            TaiKhoanDoanhNghiepDTO tkdn = new TaiKhoanDoanhNghiepDTO();
            tkdn.Idtaikhoan = dt.Rows[i]["IDTAIKHOAN"].ToString();
            tkdn.Tendn = dt.Rows[i]["TENDN"].ToString();
            tkdn.Diachi = dt.Rows[i]["DIACHI"].ToString();
            tkdn.Sdt = dt.Rows[i]["SDT"].ToString();
            tkdn.Mota = dt.Rows[i]["MOTA"].ToString();


            return (object)tkdn;
        }

        public void insert(TaiKhoanDoanhNghiepDTO info)
        {
            string insertCommand = "INSERT INTO TAIKHOANDOANHNGHIEP VALUES ('" +
                info.Idtaikhoan + "','" +
                info.Tendn + "','" +
                info.Diachi + "','" +
                info.Sdt + "','" +
                info.Mota + "')";


            executeNonQuery(insertCommand);
        }

        public void update(TaiKhoanDoanhNghiepDTO info)
        {
            string updateCommand = "UPDATE TAIKHOANDOANHNGHIEP " +
                                    "SET TENDN = '" + info.Tendn + "', " +
                                    " DIACHI = '" + info.Diachi + "'," +
                                    " SDT = '" + info.Sdt + "'," +
                                    " MOTA = '" + info.Mota + "'," +
                                    " WHERE IDTAIKHOAN  = '" + info.Idtaikhoan + "'";

            executeNonQuery(updateCommand);
        }

        public void delete(string idtk)
        {
            string deleteCommand = "DELETE FROM TAIKHOANDOANHNGHIEP WHERE IDTAIKHOAN = '" + idtk + "'";
            executeNonQuery(deleteCommand);
        }
    }
}